package cn.sxt.yxl.head.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sxt.yxl.common.controller.BaseController;
import cn.sxt.yxl.common.util.ContantFinalUtil;
import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.question.pojo.AColumns;
import cn.sxt.yxl.question.pojo.AItems;
import cn.sxt.yxl.question.pojo.ColumnsEnum;
import cn.sxt.yxl.question.pojo.ItemsEnum;
import cn.sxt.yxl.question.pojo.QuestionEnum;
import cn.sxt.yxl.question.service.IQuestionService;

@Controller
public class IndexHeaderController extends BaseController {

	@Resource
	private IQuestionService questionService;
	/**
	 * 跳转首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main() {
		ContantFinalUtil.LOGGER.info("========main======");
		return "head/main";
	}
	
	@RequestMapping("/{url}")
	public String itemsList(HttpServletRequest request,@PathVariable String url) {
		ContantFinalUtil.LOGGER.info("========itemsList======url:{}",url);
		Map<String, Object> condMap = new HashMap<>();
		condMap.clear();
		condMap.put("url",url);
		/*利用url获取栏目*/
		AColumns columns = this.questionService.findOneAColumns(condMap);
		request.setAttribute("columns", columns);
		condMap.clear();
		condMap.put("columnsId",columns.getId());
		condMap.put("status",ItemsEnum.STATUS_ENABLE.getStatus());
		PageInfoUtil pageInfoUtil = this.getPageInfoUtil(request) ;
		List<AItems> itemsList = this.questionService.findCondListItemsService(pageInfoUtil, condMap);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		request.setAttribute("itemsList", itemsList);
		return "head/itemsList";
	}
	
	@RequestMapping("/itemsInfo/{iid}")
	public String itemsInfo(HttpServletRequest request,@PathVariable int iid) {
		ContantFinalUtil.LOGGER.info("=========itemsInfo========");
		Map<String, Object> condMap = new HashMap<>();
		condMap.put("id",iid);
		/*查询出当前测试题*/
		AItems items = this.questionService.findOneItems(condMap);
		request.setAttribute("items", items);
		/*查询出上一题*/
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		
		condMap.clear();
		condMap.put("pubTimeDa",items.getPubTime());
		condMap.put("orderby","pubTimeASC");
		List<AItems> itemsPre = this.questionService.findCondListItemsService(pageInfoUtil, condMap);
		request.setAttribute("itemsPre", itemsPre);
		/*查询出下一题*/
		condMap.clear();
		condMap.put("pubTimeXa",items.getPubTime());
		List<AItems> itemsNext = this.questionService.findCondListItemsService(pageInfoUtil, condMap);
		request.setAttribute("itemsNext", itemsNext);
		return "head/itemsInfo";
	}
	/**
	 * 返回测试项下的问题
	 * @param request
	 * @return
	 */
	@RequestMapping("/questionList/{iid}/start")
	public String questionList(HttpServletRequest request, @PathVariable int iid) {
		Map<String, Object> condMap = new HashMap<>();
		condMap.clear();
		condMap.put("id",iid);
		AItems items = this.questionService.findOneItems(condMap);
		request.setAttribute("items", items);
		return "head/questionList";
		
	}
	
	/**
	 * 将查询的测试栏目动态返回
	 * @param request
	 * @return
	 */
	@RequestMapping("/header")
	public String header(HttpServletRequest request) {
		String id = request.getParameter("id");
		ContantFinalUtil.LOGGER.info("========header======id:{}",id);
		Map<String, Object> condMap = new HashMap<>();
		/*查询启用态的栏目*/
		condMap.put("status",ColumnsEnum.STATUS_ENABLE.getStatus());
		List<AColumns> columnsList = this.questionService.findCondListAcolumnsService(null, condMap);
		request.setAttribute("columnsList", columnsList);
		return "head/header";
	}
	
}
